from django.db import models

# Create your models here.

class Base(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    id=models.AutoField(primary_key=True,verbose_name='主键')

    class Meta:
        abstract = True



class User(Base):
    username = models.CharField(max_length=20,verbose_name='用户名',unique=True)
    password = models.CharField(max_length=20,verbose_name='密码')
    class Meta:
        db_table = 'user'
        verbose_name = '用户'
    def __str__(self):
        return self.username

class Goods(Base):
    name = models.CharField(max_length=20,verbose_name='商品名称',unique=True)
    price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='商品价格')
    picture = models.ImageField(upload_to='goods',verbose_name='商品图片')
    description = models.CharField(max_length=200,verbose_name='商品描述')
    category = models.ForeignKey('Category',on_delete=models.CASCADE,verbose_name='商品分类')
    stock = models.IntegerField(default=0,verbose_name='商品库存')
    class Meta:
        db_table = 'goods'
        verbose_name = '商品'

    def __str__(self):
        return self.name

class Order(Base):
    order_choices = (
        (0,'未支付'),
        (1,'已支付'),
    )
    user = models.ForeignKey('User',on_delete=models.CASCADE,verbose_name='用户')
    total_price = models.DecimalField(max_digits=10,decimal_places=2,verbose_name='总价')
    status = models.IntegerField(default=0,verbose_name='订单状态',choices=order_choices)
    class Meta:
        db_table = 'order'
        verbose_name = '订单'

    def __str__(self):
        return str(self.id)+"-"+str(self.user)

class OrderGoods(Base):
    order = models.ForeignKey('Order',on_delete=models.CASCADE,verbose_name='订单')
    goods = models.ForeignKey('Goods',on_delete=models.CASCADE,verbose_name='商品')
    number = models.IntegerField(default=0,verbose_name='商品数量')
    class Meta:
        db_table = 'order_goods'
        verbose_name = '订单商品'
        unique_together = ('order','goods')

    def __str__(self):
        return str(self.id)+str(self.order)+str(self.goods)

class Category(Base):
    name = models.CharField(max_length=20,verbose_name='分类名称',unique=True)
    class Meta:
        db_table = 'category'
        verbose_name = '分类'

    def __str__(self):
        return self.name

class Recommend(Base):
    recommend_choices = (
        (0,'店长推荐'),
        (1,'热销商品'),
    )
    goods = models.ForeignKey('Goods',on_delete=models.CASCADE,verbose_name='商品',unique=True)
    status = models.IntegerField(default=0,verbose_name='推荐类型',choices=recommend_choices)
    class Meta:
        db_table = 'recommend'
        verbose_name = '推荐'

    def __str__(self):
        return str(self.id)+str(self.goods)